<?php

/*
 * Управление номерами газеты
 */

namespace APPS\NEWSPAPER\CONTROLLERS\ADMIN;

use T4\Exception;
use T4\FS\Uploader;

use APPS\NEWSPAPER\MODELS\Issue;

class Issues extends \T4\Controller {
    
    public function actionDefault() {
	
	$issues = Issue::findAll();
	$this->view->assign('issues', $issues);

        $this->view->assign('issue', new Issue);
	
	$result['content'] = $this->view->render('admin/issues.html');
        
        return $result;
	
    }
    
    public function actionSave() {
	
	if ( !empty($_REQUEST['id']) )
	    $issue = Issue::findByPK(intval($_REQUEST['id']));
	else
	    $issue = new Issue;
	
        try {
	
            $issue->number = $_POST['number'];
            $issue->firstdate = $_POST['firstdate'];
            $issue->lastdate = $_POST['lastdate'];
            $issue->maintitle = $_POST['maintitle'];

            $uploader = new Uploader();
            $uploader->setModel($issue);
            $uploader->setUploadPath('newspaper/issues');

            $issue->pdf		= $uploader('pdf');
            $issue->image	= $uploader('image');
            $issue->mainimage	= $uploader('mainimage');

            $issue->save();
            
            $result['content'] = 'Сохранено успешно';
            $result['return'] = '/admin/newspaper/Issues';
            return $result;
        
        } catch ( Exception $e ) {

            $result['errors'][] = $e->getMessage();

        }
	
        $this->view->assign('issue', $issue);
        $result['content'] = $this->view->render('admin/issue.html');
        return $result;
	
    }
    
    public function actionEdit() {
        
        $issue = Issue::findByPK(intval($_REQUEST['id']));
	$this->view->assign('issue', $issue);
	
        $result['content'] = $this->view->render('admin/issue.html');
        return $result;
        
    }
    
}